package com.yaozheng.vocationaltraining.adapter;

import android.app.Activity;
import android.os.CountDownTimer;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.bean.PKMorePersonBean;
import com.yaozheng.vocationaltraining.bean.PKPlayerStatusBean;
import com.yaozheng.vocationaltraining.fragment.exam.PKMorePersonHallFragment;
import com.yaozheng.vocationaltraining.fragment.exam.PKMorePersonHallFragment_;
import com.yaozheng.vocationaltraining.utils.BitmapLoader;
import com.yaozheng.vocationaltraining.widget.CircleImageView;

import java.util.List;

/**
 * Created by moon on 2017/11/16.
 */

public class PKMorePersonListAdapter extends BaseAdapter {
    private LayoutInflater mInflater;//
    private PKMorePersonHallFragment mContext;
    private BitmapLoader userBitmapLoader;

    //用于退出 Activity,避免 Countdown，造成资源浪费。
    private SparseArray<CountDownTimer> countDownCounters;

    private List<PKMorePersonBean> data = null;

    private int selectedPosition = -1;

    public PKMorePersonListAdapter(PKMorePersonHallFragment context,
                                 List<PKMorePersonBean> data) {
        this.mInflater = LayoutInflater.from(context.getActivity());
        this.mContext = context;
        this.data = data;
        this.countDownCounters = new SparseArray<>();
        userBitmapLoader = new BitmapLoader(Glide.with(context), R.drawable.user_img);
    }

    /**
     * 清空当前 CountTimeDown 资源
     */
    public void cancelAllTimers() {
        if (countDownCounters == null) {
            return;
        }
        //Log.e("TAG",  "size :  " + countDownCounters.size());
        for (int i = 0, length = countDownCounters.size(); i < length; i++) {
            CountDownTimer cdt = countDownCounters.get(countDownCounters.keyAt(i));
            if (cdt != null) {
                cdt.cancel();
            }
        }
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(
                    R.layout.pk_more_person_list_item, null);
            holder = new ViewHolder();

            holder.userAvatarImage = (CircleImageView) convertView
                    .findViewById(R.id.userAvatarImage);
            holder.desc_textview = (TextView) convertView
                    .findViewById(R.id.desc_textview);
            holder.remaining_time_textview = (TextView) convertView
                    .findViewById(R.id.remaining_time_textview);
            holder.remaining_time__danwei_textview = (TextView) convertView
                    .findViewById(R.id.remaining_time__danwei_textview);
            holder.part_in_button = (Button) convertView
                    .findViewById(R.id.part_in_button);

            convertView.setTag(holder);//
        } else {
            holder = (ViewHolder) convertView.getTag();//
        }

        //加载图片
        userBitmapLoader.displayImage(data.get(position).getGameInitiatorAvatar(), holder.userAvatarImage);
        if (data.get(position).getGameStatus() == 0){//0 未开始 1 比赛中
            holder.desc_textview.setText(data.get(position).
                    getGameInitiatorName()+"发起了一个多人PK，离比赛还剩");
            holder.remaining_time_textview.setText(data.get(position)
                    .getRemaining()+"");
            holder.remaining_time_textview.setVisibility(View.VISIBLE);
            holder.remaining_time__danwei_textview.setVisibility(View.VISIBLE);
        }else if (data.get(position).getGameStatus() == 1){
            holder.desc_textview.setText(data.get(position).
                    getGameInitiatorName()+"已经发起了一个多人PK,已经开始了");
            holder.remaining_time_textview.setVisibility(View.GONE);
            holder.remaining_time__danwei_textview.setVisibility(View.GONE);
        }

        CountDownTimer countDownTimer = countDownCounters.get(holder.remaining_time_textview.hashCode());
        if (countDownTimer != null) {
            //将复用的倒计时清除
            countDownTimer.cancel();
        }
        countDownTimer = new CountDownTimer(data.get(position).getRemaining() * 1000, 1000) {
            public void onTick(long millisUntilFinished) {
                holder.remaining_time_textview.setText((millisUntilFinished / 1000)+"");
            }
            public void onFinish() {
                holder.remaining_time_textview.setText("0");
            }
        }.start();
        //将此 countDownTimer 放入list.
        countDownCounters.put(holder.remaining_time_textview.hashCode(), countDownTimer);



        holder.part_in_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mContext.partInPK(data.get(position).getGameId(),data.get(position).getId()
                        ,Integer.parseInt(holder.remaining_time_textview.getText().toString()));
            }
        });

        return convertView;
    }

    public void setData(List<PKMorePersonBean> data) {
        this.data = data;
    }

    public void setselectedPosition(int selectedPosition) {
        notifyDataSetChanged();
    }

    class ViewHolder {
        CircleImageView userAvatarImage;
        TextView desc_textview;
        TextView remaining_time_textview;
        TextView remaining_time__danwei_textview;
        Button part_in_button;
    }
}
